var file_util = require('../public/file_util');
var path = require('path');
const Define = require('./ExportDefine')

function ExportManager(list, headConfig, class_path) {
    this.class_path = class_path;
    this.headConfig = headConfig;
    this.list = list;
    this.managerMap = {}
}

ExportManager.prototype.setManager = function (managerName) {
    // console.log('manager name is ', managerName)
    if (managerName && !this.managerMap[managerName]) {
        this.managerMap[managerName] = {}
        this.managerMap[managerName][Define.HEAD] = ''
        this.managerMap[managerName][Define.INIT] = ''
        this.managerMap[managerName][Define.FUNCTION] = ''
        this.managerMap[managerName][Define.IMPORT] = ''
    }
}

ExportManager.prototype.export = function (managerTemplate) {
    //导出模型管理器
    for (var i = 1; i < this.list.length; i++) {
        if (!this.headConfig[i]) {
            continue;
        }
        var key = this.headConfig[i][Define.MGR_NAME]
        let value = this.managerMap[key]
        let export_path = this.class_path;
        if (key) {
            export_path = path.join(this.class_path, key)
        }
        file_util.createDir(export_path)
        if (value) {
            let className = key.replace(key[0], key[0].toUpperCase()) + 'Mgr';
            let classObj = managerTemplate.replace(/%ClassName%/g, className)
            classObj = classObj.replace('%init%', value[Define.INIT])
            classObj = classObj.replace('%head%', value[Define.HEAD])
            classObj = classObj.replace('%function%', value[Define.FUNCTION])
            classObj = classObj.replace('%import%', value[Define.IMPORT])
            file_util.writeFile(path.join(export_path, className + ".ts"), classObj);
        }
    }
}

module.exports = ExportManager; 